var gulp = require('gulp'),
    //删除
    del = require('del');
    // 压缩CSS
    cssmin = require('gulp-cssmin'),
    // 文件替换
    useref = require('gulp-useref'),
    // 压缩图片
    imagemin = require('gulp-imagemin'),
    // js混淆
    uglify = require('gulp-uglify'),
    // 类型筛选
    gulpif = require('gulp-if'),
    htmlmin = require('gulp-htmlmin'),
    // 指纹命名
    rev = require('gulp-rev'),
    // html中文件名替换
    revCollector = require('gulp-rev-collector'),
    // 重命名
    rename = require('gulp-rename'),
    // 替换
    replace = require('gulp-replace-task');

var stripDebug = require('gulp-strip-debug');

// 删除打包后的文件夹
gulp.task("del", function() {
    del(["./disk/ebook/"])
});
// 定义CSS任务
gulp.task('css', function () {
    return gulp.src(['./src/**/*.css'], {
            base: './src'
        })
        .pipe(rev())
        .pipe(cssmin())
        .pipe(gulp.dest('./disk/ebook'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./src/rev/css'))
});

// 定义image任务
gulp.task('image', function () {
    return gulp.src(['./src/**/*.png', './src/**/*.jpg', './src/**/*.gif'], {
            base: './src'
        })
        // .pipe(imagemin())
        .pipe(gulp.dest('./disk/ebook'))
});

// js
gulp.task('js', function () {
    return gulp.src(['./src/**/*.js'], {
            base: './src'
        })
        .pipe(rev())
        .pipe(uglify({
            mangle: true, //类型：Boolean 默认：true 是否修改变量名  
            compress: true, //类型：Boolean 默认：true 是否完全压缩  
            preserveComments: 'license' //保留所有注释  
        }))
        .pipe(stripDebug())
        .pipe(gulp.dest('./disk/ebook'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./src/rev/js'))
});

gulp.task('libs', function () {
    return gulp.src(['./src/**/*.mp3', '.src/**/*.mp4'], {
            base: './src'
        })
        // .pipe(rev())
        .pipe(gulp.dest('./disk/ebook'))
        // .pipe(rev.manifest())
        // .pipe(gulp.dest('./src/rev/libs'))
});

// gulp.task('fonts', function () {
//     return gulp.src(['./src/css/font/*'],{base: './src'})
//     .pipe(gulp.dest('./disk/ebook'))
// })

gulp.task('htm', function () {
    var options = {
        collapseWhitespace: true,
        collapseBooleanAttributes: true,
        removeComments: true,
        removeEmptyAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        minifyJS: true,
        minifyCSS: true
    };
    gulp.src('./src/**/*.htm', {
            base: './src'
        })
        .pipe(htmlmin(options))
        .pipe(gulp.dest('./disk/ebook'));
});



gulp.task('default', ['css', 'image', 'libs','js', 'htm'], function () {
    return gulp.src(['./src/rev/**/*.json', './src/**/*.htm'])
        .pipe(revCollector({
            replaceReved: true
        })).pipe(gulp.dest('./disk/ebook'))
});